package com.uchengwang.ui.utils;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.support.v7.app.NotificationCompat;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;

import com.uchengwang.R;
import com.uchengwang.ui.cm_ui.rychat.ui.ChatActivity;


/**
 * Created by U城网 on 2018/6/1.
 * 悬浮窗
 */

public class FloatNotificationUtils {
    private static FloatNotificationUtils utils;
    private FloatNotificationUtils (){}

    private NotificationManager manager;
    private Notification notification;
    private WindowManager wm;
    private View view;
    private boolean showWm = true;
    private WindowManager.LayoutParams params;


    public static FloatNotificationUtils getUtils() {
        if (utils ==null){
            utils = new FloatNotificationUtils();
        }
        return utils;
    }

    public void createNotify(Context context){
        manager=(NotificationManager)context.getSystemService(android.content.Context.NOTIFICATION_SERVICE);

        Intent intent = new Intent(context, ChatActivity.class);
        PendingIntent pi = PendingIntent.getActivity(context, 0, intent, 0);


        NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
        Notification notification = builder.setSmallIcon(R.mipmap.ic_launcher_round)
                .setPriority(Notification.PRIORITY_DEFAULT)  //通知的优先级
                .setCategory(Notification.CATEGORY_MESSAGE)  //通知的类型
                .setContentTitle("通知")
                .setAutoCancel(true)
                .setContentIntent(pi)
                .setDefaults(NotificationCompat.DEFAULT_ALL)//设置铃声及震动效果等
                .setContentText("Heads - Up Notification on Android 5.0")
                .setFullScreenIntent(pi, true)  //不设置此项不会悬挂,false 不会出现悬挂
                .build();

        manager.notify(1, notification);

    }

    public void initWindowManager(Context context){
        wm = (WindowManager) context.getSystemService(
                Context.WINDOW_SERVICE);
        params  = new WindowManager.LayoutParams();
        //注意是TYPE_SYSTEM_ERROR而不是TYPE_SYSTEM_ALERT
        //前面有SYSTEM才可以遮挡状态栏，不然的话只能在状态栏下显示通知栏
        params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
        params.format = PixelFormat.TRANSPARENT;
        //设置必须触摸通知栏才可以关掉
        params.flags = WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
                | WindowManager.LayoutParams.FLAG_FULLSCREEN
                | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;

        // 设置通知栏的长和宽
        params.width = wm.getDefaultDisplay().getWidth();
        params.height = 200;
        params.gravity =Gravity.TOP;

    }


     public void createFloatView(Context context, String str) {

        view = LayoutInflater.from(context).inflate(R.layout.layout_float_notify, null);
        //在这里你可以解析你的自定义的布局成一个View
        if (showWm){
            wm.addView(view, params);
            showWm = false;
        }else {
            wm.updateViewLayout(view,params);
        }

        view.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        wm.removeViewImmediate(view);
                        view = null;
                    break;
                case MotionEvent.ACTION_MOVE:

                    break;
                    }
                return true;
            }
        });

    }
}

